﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace NFit.WordML
{
    public class WordFileMap
    {
        public string FileName { get; set; }
        private WordFileMapProperties _properties = new WordFileMapProperties();
        public WordFileMapProperties Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }

        private List<WordFileMapElement> _elements = new List<WordFileMapElement>();
        public List<WordFileMapElement> Elements
        {
            get { return _elements; }
            set { _elements = value; }
        }

        public WordFileMapElement AddElement(WordFileMapElement elt)
        {
            Elements.Add(elt);

            return elt;
        }

        public WordFileMapElement AddElement(string stylename)
        {
            var elt = new WordFileMapElement { StyleName = stylename };
            AddElement(elt);

            return elt;
        }

        public WordFileMapElement AddElement(string stylename, string name)
        {
            var elt = new WordFileMapElement { StyleName = stylename, Name = name };
            AddElement(elt);

            return elt;
        }

        #region Enregistrement d'une map
        public void Save(string fileName)
        {
            using (StreamWriter writer = new StreamWriter(fileName, false))
            {
                Save(writer);
            }
        }

        public void Save(Stream stream)
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                Save(writer);
            }
        }

        public void Save(TextWriter writer)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(WordFileMap));
            serializer.Serialize(writer, this);
        }
        #endregion

        #region Lecture d'une map
        public static WordFileMap Read(string fileName)
        {
            WordFileMap res = null;
            using (XmlTextReader reader = new XmlTextReader(fileName))
            {
                res = Read(reader);
            }

            return res;
        }

        public static WordFileMap Read(Stream stream)
        {
            WordFileMap res = null;
            using (XmlTextReader reader = new XmlTextReader(stream))
            {
                res = Read(reader);
            }

            return res;
        }

        public static WordFileMap Read(XmlReader reader)
        {
            WordFileMap res = null;
            XmlSerializer serializer = new XmlSerializer(typeof(WordFileMap));
            res = (WordFileMap)serializer.Deserialize(reader);
            if (res != null)
                res.Properties.Hierarchy.Init();
            return res;
        }
        #endregion
    }
}
